const Controller = require('egg').Controller;
const defaultAvatar = 'http://s.yezgea02.com/1615973940679/WeChat77d6d2ac093e247c361f0b8a7aeb6c2a.png'

class UserController extends Controller {
  async register() {
    const { ctx } = this
    const { username, password } = ctx.request.body
    if (!username || !password) {
      ctx.body = {
        code: 500,
        msg: '账号密码不能为空',
        data: null
      }
      return
    }

    const userInfo = await ctx.service.user.getUserByName(username)

    if (userInfo && userInfo.id) {
      ctx.body = {
        code: 500,
        msg: '账户名已被注册, 请重新输入',
        data: null
      }
      return
    }

    const result = await ctx.service.user.register({
      username,
      password,
      signature: '皆大欢喜',
      avatar: defaultAvatar,
      ctime: new Date()
    })

    if (result) {
      ctx.body = {
        code: 200,
        msg: '注册成功',
        data: null
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '注册失败',
        data: null
      }
    }
  }

  async login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const userinfo = await ctx.service.user.getUserByName(username)
    if (!userinfo || !userinfo.id) {
      ctx.body = {
        code: 500,
        msg: '账号不存在',
        data: null
      }
      return
    }
    if (userinfo && password != userinfo.password) {
      ctx.body = {
        code: 500,
        msg: '账号密码错误',
        data: null
      }
      return
    }

    const token = app.jwt.sign({
      id: userinfo.id,
      username: userinfo.username,
      exp: Math.floor(Date.now() / 1000 + (24 * 60 * 60))
    }, app.config.jwt.secret)

    ctx.body = {
      code: 200,
      msg: '登录成功',
      data: {
        token
      }
    }
  }

  async test() {
    const { ctx, app } = this
    const authorization = ctx.request.header.authorization
    const decode = await app.jwt.verify(authorization, app.config.jwt.secret)
    ctx.body = {
      code: 200,
      msg: '操作成功',
      data: {
        ...decode
      }
    }
  }

  async getUserInfo() {
    const { ctx, app } = this
    const token = ctx.request.header.authorization
    const decode = await app.jwt.verify(token, app.config.jwt.secret)
    const userInfo = await ctx.service.user.getUserByName(decode.username)
    ctx.body = {
      code: 200,
      msg: '请求成功',
      data: {
        id: userInfo.id,
        username: userInfo.username,
        signature: userInfo.signature || '',
        avatar: userInfo.avatar || defaultAvatar
      }
    }
  }

  async editUserInfo() {
    const { ctx, app } = this
    const { signature = '' } = ctx.request.body

    try {
      let userId
      const token = ctx.request.header.authorization
      const decode = await app.jwt.verify(token, app.config.jwt.secret)
      if (!decode) return
      userId = decode.id

      const userInfo = await ctx.service.user.getUserByName(decode.username)

      const result = await ctx.service.user.editUserInfo({
        ...userInfo,
        signature
      })

      ctx.body = {
        code: 200,
        msg: '操作成功',
        data: {
          id: userId,
          signature,
          username: userInfo.username
        }
      }
    } catch (error) {

    }
  }
}

module.exports = UserController